home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 3 of 3 / CHAPTE23 / GETTIME.C < prev    next >
C/C++ Source or Header  |  1996-04-29  |  7KB  |  229 lines

  1.  
  2. #include <windows.h>
  3. #include <stdio.h>
  4. #include "GetTime.h"
  5.  
  6.  
  7. #if defined (WIN32)
  8.     #define IS_WIN32 TRUE
  9. #else
  10.     #define IS_WIN32 FALSE
  11. #endif
  12.  
  13. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  14. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  15. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  16.  
  17.  
  18. HINSTANCE hInst;   // current instance
  19.  
  20. LPCTSTR lpszAppName = "MyApp";
  21. LPCTSTR lpszTitle   = "My Application"; 
  22.  
  23. // the rest of the stuff
  24. //......................
  25.  
  26. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  27.  
  28.  
  29. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  30.                       LPTSTR lpCmdLine, int nCmdShow)
  31. {
  32.    MSG      msg;
  33.    HWND     hWnd; 
  34.    WNDCLASS wc;
  35.  
  36.    wc.style         = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
  37.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  38.    wc.cbClsExtra    = 0;                      
  39.    wc.cbWndExtra    = 0;                      
  40.    wc.hInstance     = hInstance;              
  41.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  42.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  43.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  44.    wc.lpszMenuName  = lpszAppName;              
  45.    wc.lpszClassName = lpszAppName;              
  46.  
  47.    if ( IS_WIN95 )
  48.    {
  49.       if ( !RegisterWin95( &wc ) )
  50.          return( FALSE );
  51.    }
  52.    else if ( !RegisterClass( &wc ) )
  53.       return( FALSE );
  54.  
  55.    hInst = hInstance; 
  56.  
  57.    hWnd = CreateWindow( lpszAppName, 
  58.                         lpszTitle,    
  59.                         WS_OVERLAPPEDWINDOW, 
  60.                         CW_USEDEFAULT, 0, 
  61.                         CW_USEDEFAULT, 0,  
  62.                         NULL,              
  63.                         NULL,              
  64.                         hInstance,         
  65.                         NULL               
  66.                       );
  67.  
  68.    if ( !hWnd ) 
  69.       return( FALSE );
  70.  
  71.    ShowWindow( hWnd, nCmdShow ); 
  72.    UpdateWindow( hWnd );         
  73.  
  74.    while( GetMessage( &msg, NULL, 0, 0) )   
  75.    {
  76.       TranslateMessage( &msg ); 
  77.       DispatchMessage( &msg );  
  78.    }
  79.  
  80.    return( msg.wParam ); 
  81. }
  82.  
  83.  
  84. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  85. {
  86.     WNDCLASSEX wcex;
  87.  
  88.    wcex.style         = lpwc->style;
  89.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  90.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  91.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  92.    wcex.hInstance     = lpwc->hInstance;
  93.    wcex.hIcon         = lpwc->hIcon;
  94.    wcex.hCursor       = lpwc->hCursor;
  95.    wcex.hbrBackground = lpwc->hbrBackground;
  96.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  97.    wcex.lpszClassName = lpwc->lpszClassName;
  98.  
  99.    // Added elements for Windows 95.
  100.    //...............................
  101.    wcex.cbSize = sizeof(WNDCLASSEX);
  102.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  103.                             IMAGE_ICON, 16, 16,
  104.                             LR_DEFAULTCOLOR );
  105.             
  106.    return RegisterClassEx( &wcex );
  107. }
  108.  
  109.  
  110. #define MSG_LEN            1024
  111.  
  112.  
  113. int        nTabStop = 150;
  114. char       msg[MSG_LEN+1];
  115.  
  116. HWND       hListBox = NULL;
  117. MMRESULT   rc;
  118.  
  119. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  120. {
  121.    switch( uMsg )
  122.    {
  123.       case WM_CREATE :
  124.               // Create ListBox
  125.               //...............
  126.  
  127.               hListBox = CreateWindow( "LISTBOX", "",    
  128.                                        WS_CHILD | LBS_NOTIFY | 
  129.                                        WS_VSCROLL | WS_BORDER | 
  130.                                        WS_VISIBLE | LBS_NOINTEGRALHEIGHT |
  131.                                        LBS_USETABSTOPS, 
  132.                                        0, 0, 
  133.                                        0, 0,  
  134.                                        hWnd,              
  135.                                        (HMENU)101,              
  136.                                        hInst,         
  137.                                        NULL );
  138.  
  139.               SendMessage(hListBox, LB_SETTABSTOPS, 1, (LPARAM)&nTabStop);
  140.               break;
  141.  
  142.       case WM_SIZE :
  143.               MoveWindow( hListBox, 0, 0, 
  144.                           LOWORD( lParam ), 
  145.                           HIWORD( lParam ), TRUE );
  146.               break;
  147.  
  148.       case WM_COMMAND :
  149.               switch( LOWORD( wParam ) )
  150.               {
  151.                  case IDM_TEST:
  152.                         
  153.                         // display system elapsed time since windows was started
  154.                         //......................................................
  155.                         
  156.                         {
  157.                            MMTIME mmtime;
  158.                            DWORD  ms;
  159.  
  160.                            SendMessage(hListBox, LB_RESETCONTENT, 0, 0);
  161.                
  162.                            SendMessage(hListBox, LB_ADDSTRING, 0, 
  163.                                        (LPARAM)"Windows elapsed time since "
  164.                                                "started:\t Milliseconds:");
  165.                            SendMessage(hListBox, LB_ADDSTRING, 0, (LPARAM)"");
  166.  
  167.                            mmtime.wType = TIME_MS;
  168.                            timeGetSystemTime(&mmtime, sizeof(MMTIME) );
  169.  
  170.                            ms = timeGetTime();
  171.  
  172.                            sprintf(msg, "Using timeGetSystemTime() function:\t %ld", 
  173.                                    mmtime.u.ms);
  174.                            SendMessage(hListBox, LB_ADDSTRING, 0, (LPARAM)msg);
  175.  
  176.                            sprintf(msg, "Using timeGetTime() function:\t %ld", ms);
  177.                            SendMessage(hListBox, LB_ADDSTRING, 0, (LPARAM)msg);
  178.                         }
  179.                         break;
  180.  
  181.                  case IDM_ABOUT :
  182.                         DialogBox( hInst, "AboutBox", hWnd, About );
  183.                         break;
  184.  
  185.                  case IDM_EXIT :
  186.                         DestroyWindow( hWnd );
  187.                         break;
  188.               }
  189.               break;
  190.  
  191.       case WM_DESTROY :
  192.               PostQuitMessage(0);
  193.               break;
  194.  
  195.       default :
  196.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  197.    }
  198.  
  199.    return( 0L );               
  200. }
  201.  
  202.  
  203.  
  204.  
  205. LRESULT CALLBACK About( HWND hDlg,           
  206.                         UINT message,        
  207.                         WPARAM wParam,       
  208.                         LPARAM lParam)
  209. {
  210.    switch (message) 
  211.    {
  212.        case WM_INITDIALOG: 
  213.                return (TRUE);
  214.  
  215.        case WM_COMMAND:                              
  216.                if (   LOWORD(wParam) == IDOK         
  217.                    || LOWORD(wParam) == IDCANCEL)    
  218.                {
  219.                        EndDialog(hDlg, TRUE);        
  220.                        return (TRUE);
  221.                }
  222.                break;
  223.    }
  224.  
  225.    return (FALSE); 
  226. }
  227.  
  228.  
  229.